<template>
  <div class="home-page">
     <!-- 当你的dom是隐藏的时候，操作滚动位置是无效 -->
    <!-- tab 加上 animated 事滑动的切换，所有容器是显示的，操作有效 -->
    <van-tabs animated v-model="activeChannelIndex">
       <van-tab   v-for="item in myChannels" :key="item.id" :title="item.name">
    <ArticleList :channelId="item.id"/>
  </van-tab>
</van-tabs>
    <!-- 按钮 -->
    <div class="btn-wrapper">
      <geek-icon name="search"></geek-icon>
      <geek-icon name="channel"
      @click.native="showChannel=true"></geek-icon>
    </div>
    <!-- 弹出层 -->
    <!-- :activeChannelIndex="activeChannelIndex"
    @update:activeChannelIndex="activeChannelIndex=$event"
    可以简写为 @update.sync=activeChannelIndex
    :属性名="数据" @update:属性名="数据=$event" 可以简写成 :属性名.sync="数据"
     -->
    <ArticleChannel
    v-model="showChannel"
    :myChannels="myChannels"
    :activeChannelIndex.sync="activeChannelIndex"
     />
  </div>
</template>
<script>
import ArticleChannel from './component/article-channel.vue'
import { getMyChannel } from '@/api/channel'
import ArticleList from './component/article-list.vue'

export default {
  name: 'HomePage',
  components: {
    ArticleList,
    ArticleChannel
  },
  data () {
    return {
      myChannels: [],
      showChannel: false,
      activeChannelIndex: 0
    }
  },
  // 监听token,登录完成后,更新我的频道,退出登陆后,更新我的频道
  watch: {
    '$store.state.user.token': async function () {
      const channels = await getMyChannel()
      this.myChannels = channels
      // 强制转到激活页为0
      this.activeChannelIndex = 0
    }
  },
  // 组件缓存过之后created只会执行一次
  // 退出后执行deactived
  // 在进入执行actived

  async created () {
    // err 是错误对象  res 成功结果
    const channels = await getMyChannel()
    this.myChannels = channels
  }
}
</script>
<style scoped lang="less">
::v-deep .van-tabs {
  height: 100%;
  display: flex;
  flex-direction: column;
  .van-tabs__line {
    background: @geek-color;
    height: 2px;
    width: 32px;
  }
  .van-tab {
    color: #9EA1AE;
  }
  .van-tab--active {
    font-size: 18px;
    color: #333;
  }
  .van-tabs__wrap {
    padding-right: 86px;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
  }
   .van-tabs__content {
   flex: 1;
   overflow: hidden;
 }
 .van-tab__pane {
   height: 100%;
 }
}
.home-page {
  .btn-wrapper {
    position: absolute;
    right: 0;
    top: 0;
    width: 86px;
    height: 44px;
    background: #fff;
    display: flex;
    align-items: center;
    .geek-icon {
      flex: 1;
      text-align: center;
      font-size: 18px;
    }
    &::before {
      content: "";
      width: 20px;
      height: 44px;
      position: absolute;
      left: -20px;
      top: 0;
      background: linear-gradient(to right, rgba(255,255,255,0), #fff);
    }
  }
}
</style>
